//得到用户信息 及 登录时存储的令牌
const user = JSON.parse(localStorage.getItem('exam-data'))
const token = localStorage.getItem('token')

// 查找与token匹配的用户
function findUserByToken(user, token) {
  const index = user.findIndex(user => user.account === token)
  const matchedUser = user[index]
  return { matchedUser, index }
}
// 调用功能查找用户
// index为用户在user数组中的索引,用于历史成绩的添加
const { matchedUser, index } = findUserByToken(user, token)

if (matchedUser) {
  const { id, name, account } = matchedUser
  // 填入用户信息
  document.querySelector('.name').innerHTML = `<span>欢迎，${name}</span>`
  document.querySelector('.top_nav li:last-child').style.width = '100px'
}

document.querySelector('.exit').addEventListener('click', () => {
  localStorage.removeItem('token')
  location.href = './index.html'
})

if(!token) location.href = './index.html'
